package org.kjtc.controller;

import org.kjtc.entity.*;
import org.kjtc.service.ProductService;
import org.kjtc.web.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by qwre on 2017/12/5.
 */
@RestController
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "doGetProductModuleID")
    public Map<String, Object> doGetProductModuleID(BackLog backLog) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<BackLog> listModuleID = null;
        List<BackLog> descriptionMsg = null;
        try {
            listModuleID = productService.getProductModuleID(backLog);
            descriptionMsg = productService.getDescriptionMsg(backLog);
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("listModuleID", listModuleID);
        map.put("descriptionMsg", descriptionMsg);
        return map;
    }

    @RequestMapping(value = "doGetDescriptionMsg")
    public Map<String, Object> doGetDescriptionMsg(BackLog backLog) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<BackLog> listModuleID = null;
        try {
            listModuleID = productService.getDescriptionMsg(backLog);
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("listModuleID", listModuleID);
        return map;
    }

    @RequestMapping(value = "getVersionDetalisMsg")
    public Map<String, Object> getVersionDetalisMsg(BackLog backLog) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<BackLog> listModuleDetalis = null;
        List<FeedBack> listFeedDetalis = null;
        List<BackLog> listModuleID = null;
        List finishList = new ArrayList<>();
        int SignAll = 0;
        int CountAll = 0;
        try {
            FeedBack feedBack = new FeedBack();
            feedBack.setProductID(backLog.getProductID());
            feedBack.setProductVersionID(backLog.getProductVersionID());
            listModuleDetalis = productService.getVersionDetalisMsg(backLog);
            listFeedDetalis = productService.getModuleBackDetalis(feedBack);
            listModuleID = productService.getProductModuleID(backLog);
            Map<String, Object> mapSon = new HashMap<String, Object>();
            for (int i = 0; i < listModuleID.size(); i++) {
                int[] signArray = new int[3];
                int finishSign = 0;
                int SignCount = 0;
                for (int j = 0; j < listModuleDetalis.size(); j++) {
                    if (listModuleID.get(i).getModuleID().equals(listModuleDetalis.get(j).getModuleID())) {
                        if (listModuleDetalis.get(i).getBackLogStatus() != null && listModuleDetalis.get(i).getBackLogStatus() == 3) {
                            if (listModuleDetalis.get(j).getExpectedWorkDate() != null) {
                                finishSign += listModuleDetalis.get(j).getExpectedWorkDate();
                            }
                        }
                        if (listModuleDetalis.get(j).getExpectedWorkDate() != null) {
                            SignCount += listModuleDetalis.get(j).getExpectedWorkDate();
                        }
                    }
                }
                signArray[0] = SignCount;
                signArray[1] = finishSign;
                signArray[2] = SignCount - finishSign;
                finishList.add(signArray);
                SignAll += finishSign;
                CountAll += SignCount;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("listModuleDetalis", listModuleDetalis);
        map.put("listFeedDetalis", listFeedDetalis);
        map.put("SignList", finishList);
        map.put("SignAllList", SignAll);
        map.put("CountAllList", CountAll);
        map.put("ErrandAllList", CountAll - SignAll);
        return map;
    }

    @RequestMapping(value = "addVersionsMsg")
    public Map<String, Object> addVersionsMsg(ProductVersion productVersion) {
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        String userID = userInfo.getUserID();
        if (userID != null && userID != "") {
            productVersion.setCreateUser(userID);
            productVersion.setUpdateUser(userID);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        int count = 0;
        try {
            productVersion.setProductVersionID(new SimpleDateFormat("yyyymmddhhmmss").format(new Date()));
            count = productService.addVersionsMsg(productVersion);
            if (count > 0) {
                map.put("result", "Success");
            } else {
                map.put("result", "NotFound");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("result", "Success");
        return map;
    }

    @RequestMapping(value = "/delVersionsMsg")
    public Object delVersionsMsg(ProductVersion productVersion) {
        Map<String, Object> map = new HashMap<String, Object>();
        int count = 0;
        try {
            count = productService.delVersionsMsg(productVersion);
            if (count > 0) {
                map.put("result", "Success");
            } else {
                map.put("result", "NotFound");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("result", "Success");
        return map;
    }

    @RequestMapping(value = "/updateModuleName")
    public Map<String, Object> updateModuleName(Module module) {
        UserInfo userInfo = (UserInfo)this.getSession().getAttribute("userInfo");
        String userID = userInfo.getUserID();
        if(userID != null && userID != ""){
            module.setUpdateUser(userID);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        int count = 0;
        try {
            count = productService.updateModuleName(module);
            if (count > 0) {
                map.put("result", "Success");
            } else {
                map.put("result", "NotFound");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("result", "Success");
        return map;
    }

    @RequestMapping(value = "/deletModuleID")
    public Object deletModuleID(Module module) {
        Map<String, Object> map = new HashMap<String, Object>();
        int count = 0;
        try {
            count = productService.deletModuleID(module);
            if (count > 0) {
                map.put("result", "Success");
            } else {
                map.put("result", "NotFound");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("result", "Success");
        return map;
    }

    @RequestMapping(value = "addBackLogMsgSure")
    public Map<String, Object> addBackLogMsgSure(BackLog backLog) {
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        String userID = userInfo.getUserID();
        if (userID != null && userID != "") {
            backLog.setCreateUser(userID);
            backLog.setUpdateUser(userID);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        int count = 0;
        try {
            backLog.setBackLogID(new SimpleDateFormat("yyyymmddhhmmss").format(new Date()));
            count = productService.addBackLogMsgSure(backLog);
            if (count > 0) {
                map.put("result", "Success");
            } else {
                map.put("result", "NotFound");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("result", "Success");
        return map;
    }

    @RequestMapping(value = "addFeedBackMsgSure")
    public Map<String, Object> addFeedBackMsgSure(FeedBack feedBack) {
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        String userID = userInfo.getUserID();
        if (userID != null && userID != "") {
            feedBack.setCreateUser(userID);
            feedBack.setUpdateUser(userID);
            feedBack.setFeedBackUser(userID);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        int count = 0;
        try {
            feedBack.setFeedBackID(new SimpleDateFormat("yyyymmddhhmmss").format(new Date()));
            count = productService.addFeedBackMsgSure(feedBack);
            if (count > 0) {
                map.put("result", "Success");
            } else {
                map.put("result", "NotFound");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("result", "Success");
        return map;
    }

    @RequestMapping(value = "/updateVersionsMsg")
    public Map<String, Object> updateVersionsMsg(ProductVersion productVersion) {
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        String userID = userInfo.getUserID();
        if (userID != null && userID != "") {
            productVersion.setUpdateUser(userID);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        int count = 0;
        try {
            count = productService.updateVersionsMsg(productVersion);
            if (count > 0) {
                map.put("result", "Success");
            } else {
                map.put("result", "NotFound");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("result", "Success");
        return map;
    }

    @RequestMapping(value = "saveNewProject")
    public Map<String, Object> saveNewProject(Product product) {
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        String userID = userInfo.getUserID();
        if (userID != null && userID != "") {
            product.setUserID(userID);
            product.setCreateUser(userID);
            product.setUpdateUser(userID);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        int count = 0;
        try {
            product.setProductId(new SimpleDateFormat("yyyymmddhhmmss").format(new Date()));
            count = productService.saveNewProject(product);
            if (count > 0) {
                map.put("result", "Success");
            } else {
                map.put("result", "NotFound");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("result", "Success");
        return map;
    }

    @RequestMapping(value = "addModuleID")
    public Map<String, Object> addModuleID(Module module) {
        UserInfo userInfo = (UserInfo)this.getSession().getAttribute("userInfo");
        String userID = userInfo.getUserID();
        if(userID != null && userID != ""){
            module.setUpdateUser(userID);
            module.setCreateUser(userID);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        int count = 0;
        try {
            module.setModuleID(new SimpleDateFormat("yyyymmddhhmmss").format(new Date()));
            count = productService.addModuleID(module);
            if (count > 0) {
                map.put("result", "Success");
            } else {
                map.put("result", "NotFound");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("result", "Success");
        return map;
    }

    @RequestMapping(value = "doGetAllSprint")
    public JsonResult doGetAllSprint(String productVersionID) {
        List<Sprint> list = productService.getAllSprint(productVersionID);
        return new JsonResult(list);
    }
    @RequestMapping(value = "doDeleteProduct")
    public Map<String,Object> doDeleteProduct(String productID){
        Map<String,Object> map = new HashMap<>();
        int deleteStatus = productService.deleteProduct(productID);
        if(deleteStatus == 1){
            map.put("state","Success");
        }else  if(deleteStatus == 0){
            map.put("state","Error");
        }
        return  map;
    }
    @RequestMapping(value = "doUpdateProductName")
    public Map<String,Object> doUpdateProductName(Product product){
        Map<String,Object> map = new HashMap<>();
        int updateProductNameStatus = productService.doUpdateProductName(product);
        System.out.println("updateProductNameStatus: "+updateProductNameStatus);
        if(updateProductNameStatus == 1){
            map.put("state","Success");
        }else  if(updateProductNameStatus == 0){
            map.put("state","Error");
        }
        return  map;
    }
}
